/**
 * @description: 请求监听器
 * @file: emiter.ts
 * @author: Evan
 * @date: 2025.04.09 10:19:31
 * @week: 周四
 * @version: V
 * */

// 事件名称
const eventNames = [
  'api:start', // 开始
  'api:end', // 结束
  'api:un_auth', // 未授权
  'api:error', // 异常-接口异常
  'api:invalid' // 无效-服务端状态码不成功
] as const

type EventNames = (typeof eventNames)[number]

class EventEmitter {
  private listteners: Record<string, Set<Function>> = eventNames.reduce(
    (
      obj: {
        [key: string]: Set<Function>
      },
      key
    ) => {
      obj[key] = new Set()
      return obj
    },
    {}
  )

  on(eventName: EventNames, listtener: Function) {
    this.listteners[eventName].add(listtener)
  }

  emit(eventName: EventNames, ...args: any[]) {
    this.listteners[eventName].forEach(listtener => listtener(...args))
  }
}

export default new EventEmitter()
